/**
 * @package Xslider - A slider plugin for jQuery
 * @version 0.5
 * @author xhowhy <http://x1989.com> 
 **/
;(function($){
	//幻灯片
	$.fn.Xslider = function(options){var settings ={
			affect: 'scrollx', //效果  有scrollx|scrolly|fade|none
			speed: 1500, //动画速度
			space: 6000, //时间间隔
			auto: true, //自动滚动
			trigger: 'mouseover', //触发事件 注意用mouseover代替hover
			conbox: '.conbox', //内容容器id或class
			ctag: 'a', //内容标签 默认为<a>
			switcher: '.switcher', //切换触发器id或class
			stag: 'a', //切换器标签 默认为a
			title:'.bantxt',//标题容器id或者class
			ttag: 'a', //标题器标签 默认为a
			current:'cur', //当前切换器样式名称
			rand:false //是否随机指定默认幻灯图片
		};
		settings = $.extend({}, settings, options);
		var index = 1;
		var last_index = 0;
		var $conbox = $(this).find(settings.conbox),$contents = $conbox.find(settings.ctag);
		var $title = $(this).find(settings.title),$titles = $title.find(settings.ttag);
		var $switcher = $(this).find(settings.switcher),$stag = $switcher.find(settings.stag);
		if(settings.rand) {index = Math.floor(Math.random()*$contents.length);slide();}
		if(settings.affect == 'fade'){$.each($contents,function(k, v){(k === 0) ? $(this).css({'position':'absolute','z-index':9}):$(this).css({'position':'absolute','z-index':1,'opacity':0});
			});
		}
		$titles.eq(last_index).css('display','block')
		
		function slide(){if (index >= $contents.length) index = 0;
			$stag.removeClass(settings.current).eq(index).addClass(settings.current);
			switch(settings.affect){case 'scrollx':
					$conbox.width($contents.length*$contents.width());
					$conbox.stop().animate({left:-$contents.width()*index},settings.speed);
					break;
				case 'scrolly':
					$contents.css({display:'block'});
					$conbox.stop().animate({top:-$contents.height()*index+'px'},settings.speed);
					break;
				case 'fade':
					$contents.eq(last_index).stop().animate({'opacity': 0}, settings.speed/2).css('z-index',1)
							 .end()
							 .eq(index).css('z-index',9).stop().animate({'opacity': 1}, settings.speed/2)
					break;
				case 'none':
					$contents.hide().eq(index).show();
					break;
			}
			$titles.eq(last_index).stop().animate({'opacity': 1}, settings.speed/2).hide().css('z-index',1)
			 .end()
			 .eq(index).css('z-index',9).stop().animate({'opacity': 1}, settings.speed/2).show();
			last_index = index;
			index++;
					};
		if(settings.auto) var Timer = setInterval(slide, settings.space);
		$stag.bind(settings.trigger,function(){_pause()
			index = $(this).index();
			slide();
			_continue()
		});
		$conbox.hover(_pause,_continue);
		function _pause(){
			clearInterval(Timer);
		}
		function _continue(){
			if(settings.auto)Timer = setInterval(slide, settings.space);
		}	
	}
	
	//滚动
	$.fn.extend({
        Scroll:function(opt,callback){
			//参数初始化
			if(!opt) var opt={};
			var _btnUp = $(opt.up);//Shawphy:向上按钮
			var _btnDown = $(opt.down);//Shawphy:向下按钮
			var timerID;
			var _this=this.eq(0).find("ul:first");
			var     lineH=_this.find("li:first").height(), //获取行高
					line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数，默认为一屏，即父容器高度
					speed=opt.speed?parseInt(opt.speed,10):500; //卷动速度，数值越大，速度越慢（毫秒）
					timer=opt.timer //?parseInt(opt.timer,10):3000; //滚动的时间间隔（毫秒）
			if(line==0) line=1;
			var upHeight=0-line*lineH;
			//滚动函数
			var scrollUp=function(){
					_btnUp.unbind("click",scrollUp); //Shawphy:取消向上按钮的函数绑定
					_this.animate({
							marginTop:upHeight
					},speed,function(){
							for(i=1;i<=line;i++){
									_this.find("li:first").appendTo(_this);
							}
							_this.css({marginTop:0});
							_btnUp.bind("click",scrollUp); //Shawphy:绑定向上按钮的点击事件
					});

			}
			//Shawphy:向下翻页函数
			var scrollDown=function(){
					_btnDown.unbind("click",scrollDown);
					for(i=1;i<=line;i++){
							_this.find("li:last").show().prependTo(_this);
					}
					_this.css({marginTop:upHeight});
					_this.animate({
							marginTop:0
					},speed,function(){
							_btnDown.bind("click",scrollDown);
					});
			}
		   //Shawphy:自动播放
			var autoPlay = function(){
					if(timer)timerID = window.setInterval(scrollUp,timer);
			};
			var autoStop = function(){
					if(timer)window.clearInterval(timerID);
			};
			 //鼠标事件绑定
			_this.hover(autoStop,autoPlay).mouseout();
			_btnUp.click( scrollUp ).hover(autoStop,autoPlay);//Shawphy:向上向下鼠标事件绑定
			_btnDown.click( scrollDown ).hover(autoStop,autoPlay);
			timerID = window.setInterval(scrollUp,timer);


        }      
	})
})(jQuery);
